package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.common.domesticportmanagement.DomesticPortManagementService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/domestic")
public class DomesticPortManagementController extends BaseController {
    @Resource
    private DomesticPortManagementService domesticservice;
    String menuUrl = "domestic/management";//菜单地址(权限用)

    @RequestMapping(value = "/management")
    public ModelAndView domestic(Page page) {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try {
            pd = this.getPageData();
            String HARBOR_NAME = pd.getString("HARBOR_NAME");  //判断港区名称
            if (null != HARBOR_NAME && !"".equals(HARBOR_NAME)) {
                pd.put("HARBOR_NAME", HARBOR_NAME.trim());
            }
            String HARBOR_ENGLISH_NAME = pd.getString("HARBOR_ENGLISH_NAME");  //判断港区英文名
            if (null != HARBOR_ENGLISH_NAME && !"".equals(HARBOR_ENGLISH_NAME)) {
                pd.put("HARBOR_ENGLISH_NAME", HARBOR_ENGLISH_NAME.trim());
            }
            String EDI_CODE = pd.getString("EDI_CODE"); //判断EDT
            if (null != EDI_CODE && !"".equals(EDI_CODE)) {
                pd.put("EDI_CODE", EDI_CODE.trim());
            }
            String PORT = pd.getString("PORT"); //判断港口
            if (null != PORT && !"".equals(PORT)) {
                pd.put("PORT", PORT.trim());
            }
            String CUSTOMS_CODE = pd.getString("CUSTOMS_CODE"); //判断关区代码
            if (null != CUSTOMS_CODE && !"".equals(CUSTOMS_CODE)) {
                pd.put("CUSTOMS_CODE", CUSTOMS_CODE.trim());
            }
            String FAST_SEARCH_CODE = pd.getString("FAST_SEARCH_CODE"); //判断检索码
            if (null != FAST_SEARCH_CODE && !"".equals(FAST_SEARCH_CODE)) {
                pd.put("FAST_SEARCH_CODE", FAST_SEARCH_CODE.trim());
            }
            page.setPd(pd);
            List<PageData> list = domesticservice.seach(page);
            mv.addObject("list", list);
            mv.addObject("pd", pd);
            mv.setViewName("/business/common/domesticportmanagement/domesticportmanagement");
        } catch (Exception e) {

            e.printStackTrace();
        }


        return mv;
    }


    //点添加新增一条
    @RequestMapping(value = "/adddomestic")
    @ResponseBody
    public String addship() {
        PageData pd = new PageData();
        pd = this.getPageData();
        try {
            if (pd.getString("ID") == null || pd.getString("ID") == "") {
                domesticservice.addDomestic(pd);
            } else {
                domesticservice.updateDomestic(pd);
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;

    }

    //修改页面
    @RequestMapping(value = "/editdomestic")
    public ModelAndView shipedit() {
        ModelAndView mv = this.getModelAndView();
        try {
            PageData pd = new PageData();
            pd = this.getPageData();
            pd = domesticservice.seacById(pd);
            mv.addObject("pd", pd);
            mv.setViewName("/business/common/domesticportmanagement/domesticportmanagement_edit");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    //删除
    @RequestMapping(value = "deletedomestic")
    @ResponseBody
    public String deleteU() {
        PageData pd = new PageData();
        pd = this.getPageData();
        try {
            domesticservice.delete(pd);

        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;

    }

    //批量删除
    @RequestMapping(value = "/deleteInfo")
    @ResponseBody
    public String deleteinfo() {
        PageData pd = new PageData();
        pd = this.getPageData();
        try {

            String data = pd.getString("dataJsonStr");

            JSONArray items = JSONArray.fromObject(data);
            for (int i = 0, size = items.size(); i < size; i++) {
                JSONObject item = items.getJSONObject(i);
                String id = item.getString("ID").trim();
                PageData pdq = new PageData();
                pdq.put("ID", id);
                domesticservice.delete(pdq);
            }
        } catch (Exception e) {

            e.printStackTrace();
        }

        return null;


    }

}
